﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Newtonsoft.Json;

namespace wd_3_gitlab_task
{
    public class CategoryInfo
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }

    public class TaskID
    {
        public string taskid { get; set; }  // this name must be same with json name
        public string taskname { get; set; }
    }

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        const int TASK_HOUR = 10;
        public string GetConfig()
        {
            string str = "";
            FileStream fs = null;
            string json_file = @"gconfig.json";

            try
            {
                fs = new FileStream(json_file, FileMode.Open);
                byte[] s = new byte[1024];
                fs.Read(s, 0, 1024);
                string jstr = Encoding.UTF8.GetString(s);
                //string jstr = "{\"taskid\":51460}";
                TaskID ti = JsonConvert.DeserializeObject<TaskID>(jstr);    //
                str = ti.taskid;



                //Console.WriteLine(readStr);
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }

            //Console.ReadKey();

            //原文链接：https://blog.csdn.net/qq_27508477/article/details/87557215

            return str;
        }

        public TaskID GetConfigEx()
        {
            TaskID re = new TaskID();

            try
            {
                FileStream fs = new FileStream(gitlabutils.JSON_FILE_NAME, FileMode.Open);
                byte[] s = new byte[gitlabutils.JSON_SIZE];
                fs.Read(s, 0, gitlabutils.JSON_SIZE);
                string jstr = Encoding.UTF8.GetString(s);
                re = JsonConvert.DeserializeObject<TaskID>(jstr);
                fs.Close();
            }
            catch (Exception e)
            {
                string es = e.Message.ToString();
            }

            return re;
        }

        public MainWindow()
        {
            InitializeComponent();

            //string ss = GetConfig();    //读到json串
            //this.taskid.Text = ss;

            TaskID tid = GetConfigEx();
            this.taskid.Text = tid.taskid;

            this.commenth.Text = string.Format("{{\"details\":{0}\", start.\"}}", tid.taskname);
            this.commenth3.Text = string.Format("{{\"details\":{0}\", finished.\"}}", tid.taskname);

            // init combobox
            List<CategoryInfo> categoryList = new List<CategoryInfo>();

            for (int i = 0; i < TASK_HOUR + 1; i++)
            {
                categoryList.Add(new CategoryInfo { Name = string.Format("{0}h", i), Value = i });//
            }
            cmbox_costh1.ItemsSource = categoryList;
            cmbox_costh1.DisplayMemberPath = "Name";
            cmbox_costh1.SelectedValuePath = "Value";
            cmbox_costh1.SelectedIndex = 4;

            cmbox_lefth1.ItemsSource = categoryList;
            cmbox_lefth1.DisplayMemberPath = "Name";
            cmbox_lefth1.SelectedValuePath = "Value";
            cmbox_lefth1.SelectedIndex = 1;

            cmbox_lefth2.ItemsSource = categoryList;
            cmbox_lefth2.DisplayMemberPath = "Name";
            cmbox_lefth2.SelectedValuePath = "Value";
            cmbox_lefth2.SelectedIndex = 3; // 默认选中

            cmbox_costh2.ItemsSource = categoryList;
            cmbox_costh2.DisplayMemberPath = "Name";
            cmbox_costh2.SelectedValuePath = "Value";
            cmbox_costh2.SelectedIndex = 1; // 默认选中

            cmbox_costh3.ItemsSource = categoryList;
            cmbox_costh3.DisplayMemberPath = "Name";
            cmbox_costh3.SelectedValuePath = "Value";
            cmbox_costh3.SelectedIndex = 3; // 默认选中3
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string taskid = this.taskid.Text;
            string costh = this.cmbox_costh1.SelectedValue.ToString();
            string lefth = this.cmbox_lefth1.SelectedValue.ToString();
            string commenth = this.commenth.Text;
            // Start Task #15696 Cost:1h Left:4h upload talkieserver codes by wangfeng

            string vv = string.Format("Start Task #{0} Cost:{1}h Left:{2}h {3}", taskid, costh, lefth, commenth);

            Clipboard.SetText(vv);  // copy to clip board.
            MessageBox.Show(vv);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string taskid = this.taskid.Text;
            string costh = this.cmbox_costh2.SelectedValue.ToString();
            string lefth = this.cmbox_lefth2.SelectedValue.ToString();
            string commenth = this.commenth2.Text;

            // Start Task #15696 Cost:1h Left:4h upload talkieserver codes by wangfeng

            string vv = string.Format("Effort Task #{0} Cost:{1}h Left:{2}h {3}", taskid, costh, lefth, commenth);

            Clipboard.SetText(vv);  // copy to clip board.
            MessageBox.Show(vv);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string taskid = this.taskid.Text;
            string costh = this.cmbox_costh3.SelectedValue.ToString();
            string commenth = this.commenth3.Text;

            // Start Task #15696 Cost:1h Left:4h upload talkieserver codes by wangfeng

            string vv = string.Format("Finish Task #{0} Cost:{1}h {2}", taskid, costh, commenth);

            Clipboard.SetText(vv);  // copy to clip board.
            MessageBox.Show(vv);
        }

        private void Button_Click_Folder(object sender, RoutedEventArgs e)
        {
            // get work directory - https://blog.csdn.net/qq_36628003/article/details/88684562
            string dir = System.Environment.CurrentDirectory;
            //MessageBox.Show(di);
            System.Diagnostics.ProcessStartInfo sdp = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
            sdp.Arguments = "/e," + dir;
            System.Diagnostics.Process.Start(sdp);
        }
    }
}
